package afcp.alumni.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Conf {

	public static final Charset DEFAULT_CHARSET = Charsets.UTF8;
	public static final String DEFAULT_CHARSET_NAME = Charsets.UTF8_NAME;

	private static final Log logger = LogFactory.getLog(Conf.class);

	private static final String CONFIGURATION_FILE = "config.properties";
	private static Conf instance = null;

	private Conf() {
		try {
			config.load(new InputStreamReader(this.getClass().getClassLoader()
					.getResourceAsStream(CONFIGURATION_FILE), DEFAULT_CHARSET));
			init();
		} catch (IOException e) {
			logger.fatal("Cannot load the configuration file "
					+ CONFIGURATION_FILE + "!!", e);
		}
	}
	
	private void init() {
		debug = "true".equals(_get("debug"));
	}

	protected static Conf getInstance() {
		if (instance == null) {
			instance = new Conf();
		}
		return instance;
	}

	Properties config = new Properties();

	private String _get(String key) {
		return config.getProperty(key);
	}
	public static String get(String key) {
		return getInstance()._get(key);
	}
	public static int getInt(String key) {
		try {
			return Integer.parseInt(get(key));
		} catch (NumberFormatException e) {
			logger.error("The configuration with key [" + key + "] and value ["
					+ get(key)
					+ "] is not an integer, but it is accessed as if it was.", e);
			throw e;
		}
	}
	
	private boolean debug;
	public static boolean debug() {
		return getInstance().debug;
	}
	
	public static final int BUFFER_SIZE = getInt("memberBufferSize");

}
